# -*- encoding: utf-8 -*-
"""
@File        : node_manager.py       
@License     : (C)Copyright 2021 LeeRoc (cn) Lipeng
@Modify Time : 2021/8/2 9:44 上午 
@Author      : LeeRoc
@Version     : 1.0
@Description : 
"""
import sys

sys.path.append('../')

from concurrent.futures import ThreadPoolExecutor

from core.packet_manage_unit import part_packets
from core.dc_manage_unit import host_dc_manager
from config.Config import Get_Cluster_Ip_By_Conf
from core.os_status_unit import get_localhost_ip

HOSTs = Get_Cluster_Ip_By_Conf().Get_host()
PPM = part_packets('/Volumes/MDisk/CodeLab/Python/Caltech_WebFaces')
HDM = host_dc_manager()

HDM.remote_status_update()
PPM.packeted_run()


def packeted_distribute(hostip: list):
    PPM.distributing_stragegy()

    # if HDM.host_manager.select_host_netstatus(hostip[0]) == 'ok' and HDM.host_manager.select_host_portstatus(hostip[0], 1) == 'ok':
    HDM.call_rpc_distribute(hostip, str(PPM.packeted_path + '/' + str(PPM.packet_manager.select_packet_idname_by_ip(hostip[0])[0])),
                                str(PPM.packeted_path + '/' + str(PPM.packet_manager.select_packet_idname_by_ip(hostip[0])[0])))


packeted_distribute(HOSTs[1])

# with ThreadPoolExecutor(max_workers=5) as pool:
#     for eachhost in HOSTs:
#         if eachhost[0] != get_localhost_ip():
#             try:
#                 results = pool.submit(packeted_distribute, eachhost)
#             except Exception as e:
#                 print(e)
